<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\UserBank;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use App\Models\UserBank as UserBankModel;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;


/***
 * 审核USDT钱包
 * Class UserBankController
 * @package App\Admin\Controllers
 */
class UserUsdtController extends BaseController
{
    protected $description = '用户资料审核';
    protected $title = 'USDT钱包认证';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserBank('admin_user'), function (Grid $grid) {
            $grid = $this->gridButton($grid, true, false, false, true, false);
            $grid->model()->orderBy('id', 'desc');
            $grid->tableCollapse(false);
            $grid->responsive();

            $grid->filter(function ($filter) {
                // 展开过滤器
                //$filter->expand();
                // 更改为 rightSide 布局
                $filter->panel();

                // 在这里添加字段过滤器
                $filter->like('admin_user.name', '姓名')->width(2);
                $filter->like('admin_user.username', '帐号')->width(2);
            });

            $grid->setActionClass(Grid\Displayers\ContextMenuActions::class);

            $grid->rows(function ($collection) {
                foreach ($collection as $k => $v) {
                    $v->key = $k + 1;
                }
                return $collection;
            });
            $grid->key('标识');
            $grid->model()->where('type', 2);
            $grid->column('admin_user.name', '姓名');
            $grid->column('admin_user.username', '帐号');

            $grid->usdt_id;
            $grid->usdt_img->image('', '', 30);
            $grid->status('审核状态')->using(
                [
                    UserBankModel::$status[0],
                    UserBankModel::$status[1],
                    UserBankModel::$status[2]
                ]
            )->label(['danger', 'primary', 'success']);

            $grid->created_at('申请时间');
            $grid->updated_at('处理时间')->sortable();
        });
    }


    /***
     *  重构显示页面 用Tab 方式显示多页面
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('用户资料审核')
            // body 方法可以接受 Laravel 的视图作为参数
            ->row(function (Row $row) use ($id) {
                $row->column(6, function (Column $column) use ($id) {
                    $column->append($this->detail($id));
                });
            });
    }

    /**
     * Make a show builder.
     *
     * @param  mixed  $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserBank('admin_user'), function (Show $show) {
            $show = $this->showButton($show, true);

            $show->field('admin_user.name', '姓名');
            $show->sfz_id;
            $show->sfz_zimg->image('', '', 30);
            $show->sfz_fimg->image('', '', 30);
            $show->bank_number;
            $show->bank_type;
            $show->swift_code;
            $show->status('审核状态')->using(
                [
                    UserBankModel::$status[0],
                    UserBankModel::$status[1],
                    UserBankModel::$status[2]
                ]
            )->dot(['danger', 'primary', 'success']);
            $show->created_at;
            $show->updated_at;
        });
    }


    /**
     * Edit interface.
     *
     * @param  mixed    $id
     * @param  Content  $content
     *
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description('')
            //->body($this->form()->edit($id))
            ->row(function (Row $row) use ($id) {
                $row->column(6, function (Column $column) use ($id) {
                    $column->append($this->form()->edit($id));
                });
            });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserBank(['admin_user']), function (Form $form) {
            $form = $this->formButton($form);
            $form->disableViewButton();
            $form->disableDeleteButton();
            $form->title('审核');

            $form->tab('USDT钱包信息', function ($form) {
                $form->text('admin_user.name', trans('admin-user.fields.name'))->disable();
                $form->text('usdt_id')->disable();
                $form->image('usdt_img')->disable();

                if ($form->model()->status != 1) {
                    $form->radio('status', trans('user-bank.fields.status'))
                        ->options(['0' => '未通过', '2' => '已通过'])->disable();
                    $form->disableSubmitButton();
                    $form->disableResetButton();
                } else {
                    $form->radio('status', trans('user-bank.fields.status'))
                        ->options(['0' => '驳回', '1' => '未审核', '2' => '通过']);
                }
            });


            $form->hidden('admin_id');
            $form->display('created_at');
            $form->display('updated_at');

            $form->saving(function ($form) {
                if (in_array($form->status, [1, 2])) {
                    if (!verifyFilePath('storage/'.$form->usdt_img)) {
                        return $form->error('USDT二维码错误，请驳回！');
                    }
                }
                if ($form->status == null) {
                    return $form->error('审核状态必选');
                }

                $form->admin_id = $this->getUser()->id;
                $form->usdt_img = $form->usdt_img ?? 0;
            });
            $form->saved(function ($form, $result) {
                if (!$result) {
                    return $form->error(trans('user-bank.fields.sjbcsb'));
                }

                // 获取保存后的整条数据
                $userbank = $form->repository()->eloquent();
                if ($userbank->status == 2) {
                    $userbank->admin_user->usdt_id  = $userbank->usdt_id;
                    $userbank->admin_user->usdt_img = $userbank->usdt_img;
                    $userbank->admin_user->save();
                }
            });

        });
    }


    /**
     * Create interface.
     *
     * @param  Content  $content
     *
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->title('提交银行卡')
            ->description('')
            ->body(function ($row) {
//                $row->column(4, new TotalAdminUsers());
//                $row->column(4, new NewUsers());
//                $row->column(4, new NewDevices());
            })
            ->row(function (Row $row) {
                $row->column(6, function (Column $column) {
                    $column->append($this->form());
                });
            });
    }


}
